﻿@model InvoiceModel
@using Telerik.Web.Mvc.UI;
@{
    //page title
    ViewBag.Title = T("Admin.Invoice.Create").Text;
}
@using (Html.BeginForm("Create", "Invoice"))
{
    <div class="section-header">
        <div class="title">
            <img src="@Url.Content("~/subsites/Administration/Content/images/ico-sales.png")" alt="" />
            @T("Admin.Invoice.Create") @* @Html.ActionLink("(" + T("Admin.Invoice.BackToList") + ")", "List") *@
        </div>
        <div class="options">
            <input type="submit" id="remove-orders" name="remove-orders" class="t-button" value="@T("Admin.Common.Remove")" />
            <input type="submit" id="add-orders" name="add-orders" class="t-button" value="@T("Admin.Common.AddOrders")" />
            <input type="submit" id="save-orders" name="save-continue" class="t-button" value="@T("Admin.Common.Save")" />
            <a href="@Url.Action("List")" class="t-button">@T("Admin.Common.Cancel")</a>
        </div>
    </div>

    <input type="hidden" name="selectedIds" value="" id="ciSelected" />
    @Html.Partial("_CreateOrUpdate", Model)

    <script type="text/javascript">

        var selectedIds = [];

        $(document).ready(function () {

            //"Generate invoice" button
            $('#remove-orders').click(function (e) {
                //e.preventDefault();
                //redirect to required URL
                $("#ciSelected").val(selectedIds.join(","));
                return true;
            });

            $('#mastercheckbox').click(function () {
                $('.checkboxGroups').attr('checked', $(this).is(':checked')).change();
            });

            //wire up checkboxes.
            $('#orders-grid input[type=checkbox][id!=mastercheckbox]').live('change', function (e) {
                var $check = $(this);
                if ($check.is(":checked") == true) {
                    var checked = jQuery.inArray($check.val(), selectedIds);
                    if (checked == -1) {
                        //add id to selectedIds.
                        selectedIds.push($check.val());
                    }
                }
                else {
                    var checked = jQuery.inArray($check.val(), selectedIds);
                    if (checked > -1) {
                        //remove id from selectedIds.
                        selectedIds = $.grep(selectedIds, function (item, index) {
                            return item != $check.val();
                        });
                    }
                }
                updateMasterCheckbox();
            });

            $('#save-orders').click(function () {
                var orderArr = new Array();
                $("input[name='checkedRecords']").each(function () {
                    var isChecked = $(this).is(":checked");
                    if (isChecked) {
                        orderArr.push($(this).val());
                    }
                });

                if (orderArr.length === 0) {
                    alert("Please choose an order!");
                    return false;
                } else {
                    var orders = orderArr.toString();
                    $("#OrderIds").val(orders);
                    return true;
                }
            });
            SetIframeHeightOrWidth();
        });

        function onDataBound(e) {

            $('#orders-grid input[type=checkbox][id!=mastercheckbox]').each(function () {
                var currentId = $(this).val();
                var checked = jQuery.inArray(currentId, selectedIds);
                //set checked based on if current checkbox's value is in selectedIds.
                $(this).attr('checked', checked > -1);
            });

            updateMasterCheckbox();
        }

        function updateMasterCheckbox() {
            var numChkBoxes = $('#orders-grid input[type=checkbox][id!=mastercheckbox]').length;
            var numChkBoxesChecked = $('#orders-grid input[type=checkbox][checked][id!=mastercheckbox]').length;
            $('#mastercheckbox').attr('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
        }
        function SetIframeHeightOrWidth() {
            var iframObj = $("#secureForm").find("iframe");
            iframObj.height(300);
            iframObj.width(300);
        }
    </script>

}
